package ngames.com.sushitv.action;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.FanMadeBtnAdapter;
import ngames.com.sushitv.adapter.FanMadeContentAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.ButtonHome;
import ngames.com.sushitv.entity.FanMadeContent;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.interfaces.OnItemClickListener;
import ngames.com.sushitv.temp.Interface.FanMadeInterface;
import ngames.com.sushitv.temp.Interface.FanMadeTaskInterface;
import ngames.com.sushitv.temp.Module.FanMadeFactory;


/**
 * 类似于之前的presenter
 */
public class FanMadeAction extends BaseListAction implements FanMadeTaskInterface {
    private FanMadeFactory fanMadeFactory;
    private FanMadeInterface view;
    private RecyclerView mBtnRecycler;
    private FanMadeBtnAdapter mBtnAdapter;
    private String mBtnDataID = "0";
    private RecyclerView mContentRecycler;
    private FanMadeContentAdapter mContentAdapter;
    private String mContentDataID = "0";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Log.d("FeaturedPresenter", fanMadeFactory.getBannerUrls().get(0));
                    view.initingData(fanMadeFactory.getBannerUrls(), fanMadeFactory.getFanMadeSeriesArray());
                    view.initedData();
                    break;
                case 2:
                    view.refreshedData(fanMadeFactory.getBannerUrls(), fanMadeFactory.getFanMadeSeriesArray());
                    break;
            }
        }
    };

    public FanMadeAction(Activity activity, View baseView, FanMadeFactory fanMadeFactory, FanMadeInterface view) {
        super(activity, baseView);
        this.fanMadeFactory = fanMadeFactory;
        this.view = view;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBtnRecycler = (RecyclerView) mBaseView.findViewById(R.id.recycler_fanMade_button);
        mBtnRecycler.setHasFixedSize(true);
        //mRecycler.setLayoutManager(mLinearLayoutManager);
        mBtnRecycler.setLayoutManager(new GridLayoutManager(mActivity, 3));

        mContentRecycler = (RecyclerView) mBaseView.findViewById(R.id.recycler_fanMade_content);
        mContentRecycler.setHasFixedSize(true);
        //mRecycler.setLayoutManager(mLinearLayoutManager);
        mContentRecycler.setLayoutManager(new GridLayoutManager(mActivity, 1));
        initButton();

    }
    ArrayList<FanMadeContent> fanMadeContents = new ArrayList<>();
    private void initContent(Boolean isNew, final ArrayList<ButtonHome> btnData, int i) {
        if(isNew){
            fanMadeContents = new ArrayList<>();
        }

        //for(int i=0;i<data.size();i++){
        final ButtonHome bh = btnData.get(i);
        final int finalI = i;
        getAppModel().videoRank("videos", bh.getId(), new ModelCallback<ArrayList<ItemVideo>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<ItemVideo> data) {
                FanMadeContent fanMadeContent = new FanMadeContent();
                fanMadeContent.setId(finalI + "");
                fanMadeContent.setName(bh.getName());
                fanMadeContent.setItemVideos(data);
                fanMadeContents.add(fanMadeContent);
                Log.e("itemview", finalI + "");
                if (btnData.size() > finalI+1) {
                    int i=finalI+1;
                    initContent(false,btnData,i);
                }else{
                    mIsLoadingMore = false;
                    //mSwipeRefreshLayout.setRefreshing(false);
                    if ("0".equals(mContentDataID) && mContentAdapter != null) {
                        mContentAdapter.clearData();
                    }
                    if (isEmptyList(data)) {
                        return;
                    }
                    mContentDataID = fanMadeContents.get(data.size() - 1).getId();
                    setContentListData(fanMadeContents);
                }
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                Log.e("itemviewfail", finalI + "");
            }
        });
        // }
    }

    private void initButton() {
        getAppModel().videoCates(new ModelCallback<ArrayList<ButtonHome>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<ButtonHome> data) {
                mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
                if ("0".equals(mBtnDataID) && mBtnAdapter != null) {
                    mBtnAdapter.clearData();
                }
                if (isEmptyList(data)) {
                    return;
                }
                mBtnDataID = data.get(data.size() - 1).getId();
                setBtnListData(data);
                initContent(true,data, 0);
            }

            @Override
            public void onFailure(String errorEvent, String message) {

            }
        });

    }

    @Override
    public void onRefreshData() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void initData() {
        fanMadeFactory.initData(handler);
    }

    @Override
    public void refreshData() {
        view.refresinghData();
        fanMadeFactory.refreshData(handler);
    }


    public void setBtnListData(ArrayList<ButtonHome> data) {
        if (mBtnAdapter == null) {
            mBtnAdapter = new FanMadeBtnAdapter(mActivity, data);
            mBtnAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemButtonClick(View view, int position) {

                }
            });
            mBtnRecycler.setAdapter(mBtnAdapter);
        } else {
            mBtnAdapter.addData(data);
        }
    }

    public void setContentListData(ArrayList<FanMadeContent> data) {
        if (mContentAdapter == null) {
            mContentAdapter = new FanMadeContentAdapter(mActivity,mBaseView, data);
            mContentAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemButtonClick(View view, int position) {

                }
            });
            mContentRecycler.setAdapter(mContentAdapter);
        } else {
            mContentAdapter.addData(data);
        }
    }


}
